<template>
  <div>
    <el-card class="box-card">
      <!-- 面包屑导航 -->
      <div>
        <el-breadcrumb separator="/">
          <el-breadcrumb-item>首页</el-breadcrumb-item>
          <el-breadcrumb-item>管理设置</el-breadcrumb-item>
          <el-breadcrumb-item active>权限管理</el-breadcrumb-item>
        </el-breadcrumb>
      </div>

      <el-button
        type="success"
        style="margin-top: 30px"
        @click="addDialogVisible = true"
        >添加管理员</el-button
      >
      <el-table
        :data="datalist"
        height="500"
        row-key="pid"
        border
        style="width: 100%; margin-top: 30px; height: 100%"
      >
        <el-table-column prop="uid" label="管理员编号" width="180">
        </el-table-column>
        <el-table-column prop="username" label="管理员名称" width="180">
        </el-table-column>
        <el-table-column prop="role.rolename" label="角色名称">
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              type="warning"
              @click="editDialog(scope.row)"
              icon="el-icon-edit"
            ></el-button>
            <el-button
              type="danger"
              @click="deleteDialog(scope.row)"
              icon="el-icon-delete"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 添加按钮 -->
      <el-dialog
        title="添加管理员"
        :visible.sync="addDialogVisible"
        width="30%"
        center
        :before-close="clearDialog"
      >
        <el-form :model="form" label-width="100px" class="demo-ruleForm">
          <el-form-item label="管理员名称" prop="pass">
            <el-input v-model="form.username" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="管理员密码" prop="pass">
            <el-input v-model="form.password" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="角色列表" prop="pass">
            <el-select v-model="form.role" placeholder="请选择">
              <el-option
                v-for="item in datarolelist"
                :key="item.rid"
                :label="item.rolename"
                :value="item.rid"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="clearDialog">取 消</el-button>
          <el-button type="primary" @click="addPoWer">确 定</el-button>
        </span>
      </el-dialog>
      <!-- 修改按钮 -->
      <el-dialog
        title="修改管理员"
        :visible.sync="editDialogVisible"
        width="30%"
        center
        :before-close="clearDialog"
      >
        <el-form :model="form" label-width="100px" class="demo-ruleForm">
          <el-form-item label="管理员名称" prop="pass">
            <el-input
              v-model="form.username"
              autocomplete="off"
              disabled
            ></el-input>
          </el-form-item>
          <el-form-item label="管理员密码" prop="pass">
            <el-switch
              v-model="pwd"
              active-color="#13ce66"
              inactive-color="#ff4949"
            >
            </el-switch>
            <el-input
              v-if="pwd"
              v-model="form.password"
              autocomplete="off"
              placeholder="为空默认不修改"
            ></el-input>
          </el-form-item>
          <el-form-item label="角色列表" prop="pass">
            <el-select v-model="form.role" placeholder="请选择">
              <el-option
                v-for="item in datarolelist"
                :key="item.rid"
                :label="item.rolename"
                :value="item.rid"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="clearDialog">取 消</el-button>
          <el-button type="primary" @click="editadminclick">确 定</el-button>
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import { adminlist, addadmin, editadmin, deladmin } from "../../http/api";
import { mapActions, mapGetters } from "vuex";
export default {
  data() {
    return {
      datalist: [],
      //   添加提示框
      addDialogVisible: false,
      editDialogVisible: false,
      pwd: false,
      form: {
        uid: "",
        username: "",
        role: null,
        del: null,
      },
    };
  },
  created() {
    this.getadminlist();
    this.getrolelist();
  },
  methods: {
    ...mapActions({
      getrolelist: "role/getrolelistActions",
    }),
    //   获取管理员列表
    getadminlist() {
      let that = this;
      adminlist().then((res) => {
        if (res.status == 201) {
          console.log(res);
          that.datalist = res.data;
        } else {
          that.$message.error(res.message);
        }
      });
    },
    // 添加管理员
    addPoWer() {
      let that = this;
      addadmin(this.form).then((res) => {
        if (res.status == 201) {
          //   that.datalist = res.data;
          that.$message.success(res.message);
          that.getadminlist();
        } else {
          console.log(res);
          that.$message.error(res.message);
        }
      });
      this.clearDialog();
    },
    //修改管理员
    editDialog(data) {
      console.log(data);
      this.editDialogVisible = true;
      this.form.uid = data.uid;
      this.form.username = data.username;
      this.form.role = data.role.rid;
    },
    // 确定修改
    editadminclick() {
      let that = this;
      editadmin(this.form).then((res) => {
        if (res.status == 201) {
          //   that.datalist = res.data;
          that.$message.success(res.message);
          that.getadminlist();
        } else {
          that.$message.error(res.message);
        }
      });

      this.clearDialog();
    },
    //删除管理员
    deleteDialog(data) {
      let that = this;
      this.form.del = 1;
      this.form.uid = data.uid;
      this.$confirm("此操作将永久删除该管理员, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          deladmin(this.form).then((res) => {
            if (res.status == 201) {
              //   that.datalist = res.data;
              that.$message.success(res.message);
              that.getadminlist();
            } else {
              console.log(res);
              that.$message.error(res.message);
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //清空数据
    clearDialog() {
      this.editDialogVisible = false;
      this.addDialogVisible = false;
      this.form = {
        uid: "",
        username: "",
        role: null,
        del: null,
      };
    },
  },
  computed: {
    ...mapGetters({
      datarolelist: "role/datalist",
    }),
  },
};
</script>

<style>
</style>